﻿using AutoMapper;
using BPMN.Application.Contracts.DTO.Request.Dictionary;
using BPMN.Application.Contracts.DTO.Response;
using BPMN.Application.Contracts.DTO.Response.Dictionary;
using BPMN.Application.IService;
using BPMN.Domain.Entity.SysDictonary;
using BPMN.Domain.Interface;
using BPMN.DomianShare;
using BPMN.DomianShare.PulicDto;
using BPMN.Infrastructure.Repository;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using static Dapper.SqlMapper;

namespace BPMN.Application.Service
{
    public class DitonaryAppService : IDitonaryAppService
    {
        private readonly IRepositoryBase<Dictionary> RepDic;
        private readonly IRepositoryBase<DictionaryList> RepDicList;
        private readonly IMapper mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="RepDic"></param>
        public DitonaryAppService(IRepositoryBase<Dictionary> RepDic, IMapper mapper, IRepositoryBase<DictionaryList> RepDicList)
        {
            this.RepDic = RepDic;
            this.mapper = mapper;
            this.RepDicList = RepDicList;
        }
        /// <summary>
        /// 字典信息添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> AddDitonary(DictionaryRequstDto entity)
        {
            int n = await RepDic.Add(mapper.Map<Dictionary>(entity));
            if (n > 0)
            {

                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>()
                {
                    Code = ResultCode.Fail,
                    ErrorInfo = "添加失败"
                };
            }
        }
        /// <summary>
        /// 添加字典明细（查询）
        /// </summary>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<PulicDictonaryListResponseDto>>> AddDitonaryList(DictionaryListrequestDto entity)
        {
            var slist = await RepDic.Get(entity.DicId);
            if (!string.IsNullOrEmpty(slist.DbSql))
            {
                var slist1 = RepDic.ExecSqlDictionary(slist.DbSql).Result;
                return new ResultResponseDto<List<PulicDictonaryListResponseDto>>()
                {
                    Code = ResultCode.Ok,
                    ErrorInfo = "获取成功",
                    Data = slist1
                };
            }
            else
            {
                var DicListSlist = RepDicList.GetAll().Result.Where(x => x.DicId == entity.DicId);
                if (DicListSlist.Count() > 0)
                {
                    await RepDicList.RealDel(DicListSlist.AsEnumerable());
                }

                List<DictionaryList> dictionaryLists = new List<DictionaryList>();
                List<PulicDictonaryListResponseDto> list = new List<PulicDictonaryListResponseDto>();
                foreach (var item in entity.DictionaryListAdd)
                {
                    dictionaryLists.Add(new DictionaryList { DicId = entity.DicId, DicValue = item.DicValue, DicName = item.DicName, OrderNo = item.OrderNo, Remark = item.Remark, Enable = item.Enable });
                }
                int n = await RepDicList.AddRange(dictionaryLists);
                if (n > 0)
                {
                    var slist2 = RepDicList.GetAll().Result.Where(x => x.DicId == entity.DicId).ToList();
                    foreach (var item in slist2)
                    {
                        list.Add(new PulicDictonaryListResponseDto { key = item.DicValue, value = item.DicName });
                    }
                    return new ResultResponseDto<List<PulicDictonaryListResponseDto>>()
                    {
                        Code = ResultCode.Ok,
                        ErrorInfo = "获取成功",
                        Data = list
                    };
                }
                else
                {
                    return new ResultResponseDto<List<PulicDictonaryListResponseDto>>()
                    {
                        Code = ResultCode.Fail,
                        ErrorInfo = "获取失败",
                    };
                }

            }
        }
        /// <summary>
        /// 根据字典ID获取相对用的数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<PulicDictonaryListResponseDto>>> GetDicListByDicId(int Id)
        {
            var slist = await RepDic.Get(Id);
            if (!string.IsNullOrEmpty(slist.DbSql))
            {
                var slist1 = RepDic.ExecSqlDictionary(slist.DbSql).Result;
                return new ResultResponseDto<List<PulicDictonaryListResponseDto>>()
                {
                    Code = ResultCode.Ok,
                    ErrorInfo = "获取成功",
                    Data = slist1
                };
            }
            else
            {
                var slist3 = RepDicList.GetAll().Result.Where(x => x.DicId == Id).ToList();
                List<PulicDictonaryListResponseDto> list = new List<PulicDictonaryListResponseDto>();
                foreach (var item in slist3)
                {
                    list.Add(new PulicDictonaryListResponseDto { key = item.DicValue, value = item.DicName });
                }
                return new ResultResponseDto<List<PulicDictonaryListResponseDto>>()
                {
                    Code = ResultCode.Ok,
                    ErrorInfo = "获取成功",
                    Data = list
                };
            }
        }
        /// <summary>
        /// 获取字典明细信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<DictionaryListResponseDto>>> GetDictionaryListMx(int Id)
        {
            var slist = RepDicList.GetAll().Result.Where(x => x.DicId == Id).ToList();
            return new ResultResponseDto<List<DictionaryListResponseDto>>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = mapper.Map<List<DictionaryListResponseDto>>(slist)
            };
        }
        /// <summary>
        /// 数据字典列表
        /// </summary>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<DictonartyResponseDto>>> GetDictonaryList()
        {
            return new ResultResponseDto<List<DictonartyResponseDto>>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = mapper.Map<List<DictonartyResponseDto>>(RepDic.GetAll().Result.Where(x => x.IsDel == true).ToList())
            };
        }



    }
}
